package neusoft.example.com.yuejian06.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.example.com.yuejian06.Activity.ClubDetailActivity;
import neusoft.example.com.yuejian06.Bean.Club;
import neusoft.example.com.yuejian06.PublicIp;
import neusoft.example.com.yuejian06.R;

public class ClubListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater inflater;
    private Context context;
    private List<Club> list;

    public ClubListAdapter(Context context){
        this.inflater=LayoutInflater.from(context);
        this.context=context;
    }

    public ClubListAdapter(Context context,List<Club> list){
        this.list=list;
        this.context=context;
    }

    public void setList(List<Club> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(context).inflate(R.layout.adapter_clublist,parent,false);
       RecyclerView.ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Club bean=list.get(position);
        if(bean==null) return;
        final ViewHolder viewHolder=(ViewHolder) holder;

        viewHolder.club_name.setText(bean.getClub_name());
        viewHolder.club_intro.setText(bean.getClub_intro());
        Picasso.with(context)
                .load(PublicIp.ip+"static/club_picture/"+bean.getClub_pic())
                .into(viewHolder.club_pic);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,ClubDetailActivity.class);
                intent.putExtra("club_id", bean.getClub_id());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list == null)
            return 0;
        else
            return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView club_pic;
        TextView club_name,club_intro;
        public ViewHolder(View itemView) {
            super(itemView);
            club_pic=(ImageView) itemView.findViewById(R.id.club_pic);
            club_name=(TextView) itemView.findViewById(R.id.club_name);
            club_intro=(TextView) itemView.findViewById(R.id.club_intro);
        }
    }
}
